package com.easycoding.framework.repository.support.query.expression;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Root;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.easycoding.framework.repository.support.query.QueryExpression;
import com.easycoding.framework.util.Tool;

/**
 * 字段表达式
 * @author Wangjk
 * @Date 2013-9-25
 * @Version 1.0
 */
public class FieldExpression<T> extends QueryExpression<T> {
	
	private static final Log log = LogFactory.getLog(FieldExpression.class);
	
	/**
	 * 字段名称
	 */
	private String fieldName;
	
	public FieldExpression(String fieldName){
		this.fieldName = fieldName;
	}
	
	@Override
	public Expression<T> toExpression(Root<?> root, CriteriaBuilder cb) {
		if(log.isDebugEnabled())
			log.debug("字段："+fieldName);
		if(!Tool.isEmpty(fieldName))
			return root.<T>get(fieldName);
		else
			return null;
	}

	/**
	 * 获取字段名称
	 * @return fieldName 字段名称
	 */
	public String getFieldName() {
		return fieldName;
	}

	/**
	 * 设置字段名称
	 * @param fieldName 字段名称
	 */
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

}
